package com.spring.base;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.sys.po.SysUser;
import com.spring.util.EncryptionUtil;
import com.spring.util.constant.Constant;



/**
 * Created by liuyang on 2016/7/10.
 */
public class AdminBaseController {

	protected final static Logger logger = Logger.getLogger(AdminBaseController.class);

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	@ExceptionHandler
	@ResponseBody
	public Map<String, Object> exp(Exception ex) {
		// 记录日志
		ex.printStackTrace();
		logger.error("后台异常统一捕获： "+request.getRequestURL()+"----->" + ex.getMessage(), ex);
		return errorJson("请稍后再试");
	}

	/**
	 * 后台获取当前登录用户信息
	 * 
	 * @param request
	 * @return
	 */
	public SysUser getCurrentUserInfo() {
		SysUser userInfo = (SysUser) request.getSession().getAttribute(Constant.HXP_MANAGER_USER_COOKIE_KEY);
		return userInfo;
	}


	/**
	 * 设置session值
	 * 
	 * @param key
	 * @param value
	 */
	public void setSessionAttribute(String key, Object value) {
		request.getSession().setAttribute(key, value);
	}

	/**
	 * 获取session值
	 * 
	 * @param key
	 * @param value
	 */
	public Object getSessionAttribute(String key) {
		return request.getSession().getAttribute(key);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
	}

	public String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	protected Map<String, Object> resultJson(String... kvs) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (kvs == null || kvs.length == 0) {
			return map;
		}
		for (int i = 0, len = kvs.length; (i + 1) < len; i += 2) {
			map.put(kvs[i], kvs[i + 1]);
		}
		return map;
	}

	protected Map<String, Object> successJson() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		return map;
	}

	protected Map<String, Object> successJson(String... kvs) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		if (kvs == null || kvs.length == 0) {
			return map;
		}
		for (int i = 0, len = kvs.length; (i + 1) < len; i += 2) {
			map.put(kvs[i], kvs[i + 1]);
		}
		return map;
	}

	protected Map<String, Object> successJson(String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", true);
		map.put("msg", msg);
		return map;
	}

	protected Map<String, Object> errorJson(Object msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		map.put("msg", msg);
		return map;
	}

/*	public static void main(String[] args) {
		logger.info("token:" + EncryptionUtil.MD5(ConstantsStatus.DOCT_TOKEN_KEY + "15801471128"));// md5加密的token);
	}
	*//**
	 * 删除患者用户的token
	 * 
	 * @param token
	 * @return
	 *//*
	public void delPatientToken(String token) {
		JedisManager.del(token);
	}*/
}
